package ua.kharkov.knure.pereverziev.diploma.db.entity;

import java.io.Serializable;
import java.sql.Time;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

@Entity(name = "schedule")
public class Schedule extends HasId implements Serializable {

	private static final long serialVersionUID = -1L;

	@Id
	@Column(name = "schedule_id")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@ManyToOne
	@JoinColumn(name = "departure_airport_id")
	private Airport departure;

	@ManyToOne
	@JoinColumn(name = "destination_airport_id")
	private Airport destination;

	private Integer rate;

	@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "flight_days")
	private Set<WeekDay> daysOfWeek = new HashSet<WeekDay>(0);

	@ManyToOne
	@JoinColumn(name = "airplane_id")
	private Airplane airplane;

	@Column(name = "departure_time")
	private Time departureTime;

	@Column(name = "flight_duration")
	private Time flight_duration;
	private Long distance;

	@Override
	public String toString() {
		return "\nSchedule [id=" + id + ",\n\t departure=" + departure
				+ ",\n\t destination=" + destination + ",\n\t rate=" + rate
				+ ",\n\t daysOfWeek=" + daysOfWeek + ",\n\t airplane="
				+ airplane + ",\n\t departureTime=" + departureTime + "]";
	}

	public String getShortDescription() {
		return departure
				+ " - "
				+ destination
				+ " ("
				+ departureTime
				+ " - "
				+ (new Time(departureTime.getTime() - flight_duration.getTime()))
				+ ") ";
	}

	// *********** Getters and setters *************

	@Override
	public Long getId() {
		return id;
	}

	@Override
	public void setId(final Long id) {
		this.id = id;
	}

	public Airport getDeparture() {
		return departure;
	}

	public void setDeparture(final Airport departure) {
		this.departure = departure;
	}

	public Airport getDestination() {
		return destination;
	}

	public void setDestination(final Airport destination) {
		this.destination = destination;
	}

	public Integer getRate() {
		return rate;
	}

	public void setRate(final Integer rate) {
		this.rate = rate;
	}

	public Time getDepartureTime() {
		return departureTime;
	}

	public void setDepartureTime(final Time departureTime) {
		this.departureTime = departureTime;
	}

	public Time getFlight_duration() {
		return flight_duration;
	}

	public void setFlight_duration(final Time flight_duration) {
		this.flight_duration = flight_duration;
	}

	public Long getDistance() {
		return distance;
	}

	public void setDistance(final Long distance) {
		this.distance = distance;
	}

	public Airplane getAirplane() {
		return airplane;
	}

	public void setAirplane(final Airplane airplane) {
		this.airplane = airplane;
	}

	public Set<WeekDay> getDaysOfWeek() {
		return daysOfWeek;
	}

	public void setDaysOfWeek(final Set<WeekDay> daysOfWeek) {
		this.daysOfWeek = daysOfWeek;
	}

	public String getDays() {
		if (daysOfWeek == null || daysOfWeek.size() == 0)
			return "";
		final StringBuilder result = new StringBuilder();
		for (final WeekDay day : daysOfWeek) {
			result.append(", ");
			result.append(day.getDay());
		}
		return result.toString().substring(2);
	}

}
